package com.game.screen;

import java.util.LinkedList;
import java.util.List;

import android.graphics.Color;
import android.graphics.Point;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;

import com.game.bullet.BulletManager;
import com.game.character.MonsterManager;
import com.game.character.Player;
import com.game.framwork.gamesupport.Camera;
import com.game.framwork.gamesupport.QuadTree;
import com.game.framwork.itf.Game;
import com.game.framwork.itf.Graphics;
import com.game.framwork.itf.Graphics.PixmapFormat;
import com.game.framwork.itf.Input.KeyEvent;
import com.game.framwork.itf.Input.TouchEvent;
import com.game.framwork.itf.Screen;
import com.game.tiledmap.DamgeReceiveManager;
import com.game.tiledmap.TiledMap;
import com.game_ltdd.Resrouce;

public class BattleScreen extends Screen {
	private final static int WIDTH_MAP = 20;
	private final static int HEIGHT_MAP = 20;
	private final static int CELLSIZE = 50;
	private TiledMap mTiledMap;
	private QuadTree mQuadTree;
	private Player player;
	private Camera camera;

	public BattleScreen(Game game) {
		super(game);
		initResource();
		initSound();
		initMap();
		initPlayer();
		initQuadTree();
		initMonster();
		BulletManager.getInstance().attachMap(mTiledMap);
		Resrouce.maingame.play(1, -1);
	}
	
	private void initMonster() {
		MonsterManager.getInstance().attachMap(mTiledMap);
		MonsterManager.getInstance().setPos(3, 5);
		MonsterManager.getInstance().setPos(3, 9);
		MonsterManager.getInstance().setPos(3, 2);
	}
	
	private void initSound() {
		Resrouce.bulletexplosion = game.getAudio().newSound("BombExplosion.wav");
		Resrouce.maingame = game.getAudio().newSound("MainTheme.mp3");
		Resrouce.playerdie = game.getAudio().newSound("PlayerDie.wav");
		Resrouce.setbullet = game.getAudio().newSound("SetNormalBomb.wav");
	}

	private void initResource() {
		Graphics g = game.getGraphics();
		Resrouce.player = g.newPixmap("Player.png", PixmapFormat.ARGB8888);
		Resrouce.mapObject = g
				.newPixmap("MapObject.png", PixmapFormat.ARGB8888);
		Resrouce.bulletImage = g.newPixmap("BoomT.png", PixmapFormat.ARGB8888);
		Resrouce.bulletExplore = g.newPixmap("DyingEffect.PNG",
				PixmapFormat.ARGB8888);
		Resrouce.mapObjectExplore = g.newPixmap("BreakableObj.png",
				PixmapFormat.ARGB8888);
		Resrouce.boss = g.newPixmap("Boss.PNG", PixmapFormat.ARGB8888);
		Resrouce.rageboss = g.newPixmap("BossRage.png", PixmapFormat.ARGB8888);
		Resrouce.mummy = g.newPixmap("MobMummy.PNG", PixmapFormat.ARGB8888);
		Resrouce.ragemummy = g.newPixmap("MobMummyRage.png",
				PixmapFormat.ARGB8888);
		Resrouce.bat = g.newPixmap("MobBat.PNG", PixmapFormat.ARGB8888);
	}

	private void initMap() {
		mTiledMap = new TiledMap();
		mTiledMap.initMap(WIDTH_MAP, HEIGHT_MAP, CELLSIZE);
		camera = new Camera(WIDTH_MAP * CELLSIZE, HEIGHT_MAP * CELLSIZE);
		mTiledMap.attachCamera(camera);
		mTiledMap.setMapPos(new Point(0, 0));

		int[][] data = {
				{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 },
				{ 4, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4 },
				{ 4, 4, 4, 4, 4, 0, 1, 4, 4, 4, 4, 4, 4, 4, 1, 1, 4, 4, 4, 4 },
				{ 4, 4, 4, 4, 4, 0, 1, 4, 4, 4, 4, 4, 4, 4, 1, 1, 4, 4, 4, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 7, 5, 6, 0, 0, 0, 0, 7, 5, 6, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 7, 5, 5, 0, 0, 0, 0, 7, 5, 5, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 7, 5, 6, 0, 0, 0, 0, 7, 5, 6, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 7, 5, 5, 0, 0, 0, 0, 7, 5, 5, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 5, 6, 6, 6, 6, 6, 5, 0, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 5, 6, 1, 1, 1, 6, 5, 0, 0, 0, 0, 0, 4 },
				{ 4, 0, 0, 0, 0, 0, 0, 5, 6, 1, 0, 1, 6, 5, 0, 0, 0, 0, 0, 4 },
				{ 4, 4, 4, 4, 4, 4, 4, 5, 6, 1, 0, 1, 6, 5, 4, 4, 4, 4, 4, 4 } };

		for (int i = 0; i < HEIGHT_MAP; i++) {
			for (int j = 0; j < WIDTH_MAP; j++) {
				mTiledMap.setCell(i, j, data[i][j]);
			}
		}
	}

	private void initPlayer() {
		player = new Player(game);
		player.attachTiledMap(mTiledMap);
		player.setPosInMap(16, 3);
	}

	private void initQuadTree() {
		mQuadTree = new QuadTree(10, WIDTH_MAP * CELLSIZE, HEIGHT_MAP
				* CELLSIZE, game);
		mQuadTree.addObject(player);
		mQuadTree.attachMap(mTiledMap);
	}

	float timeUpdate;

	@Override
	public void update(float deltaTime) {
		long start = System.currentTimeMillis();
		// player
		player.updateInternal(deltaTime);
		// title map
		TiledMap.updatePlayerPosToMap(player, mTiledMap);
		camera.setViewPort(player.getPossition().x, player.getPossition().y);
		// bullet
		BulletManager.getInstance().update(deltaTime);
		// monster
		MonsterManager.getInstance().update(deltaTime);
		// dame
		DamgeReceiveManager.getInstance().update(deltaTime);
		// input
		processInput(deltaTime);
		timeUpdate = (System.currentTimeMillis() - start) / 1000f;
	}

	@Override
	public void present(float deltaTime, Graphics g) {
		long start = System.currentTimeMillis();

		g.drawRect(0, 0, 800, 480, Color.BLACK);
		// tile map
		mTiledMap.render(g, deltaTime);
		// bullet
		BulletManager.getInstance().paint(g);
		// player
		player.paintInternal(g);
		// monster
		MonsterManager.getInstance().paint(g);
		// damge
		DamgeReceiveManager.getInstance().paint(g);

		float time = (System.currentTimeMillis() - start) / 1000f;
		g.drawText("Wave: " + MonsterManager.getInstance().getWave(), game.getDeviceWidth() - 200, 100, Color.BLACK, 50);
		g.drawText("Monsters: " + MonsterManager.getInstance().getNumMobs(), game.getDeviceWidth() - 300, 150, Color.BLACK, 50);
		g.drawText("time paint: " + time, 100, 100, Color.BLACK, 50);
		g.drawText("time update: " + timeUpdate, 100, 150, Color.BLACK, 50);
		g.drawText(
				"bulletList size: "
						+ BulletManager.getInstance().listBullet.size(), 100,
				200, Color.BLACK, 50);
		g.drawText("monsterList size: "
				+ MonsterManager.getInstance().getListMonster().size(), 100,
				250, Color.BLACK, 30);
		g.drawText("rageMonsterList size: "
				+ MonsterManager.getInstance().getListRageMonster().size(),
				100, 300, Color.BLACK, 30);
		g.drawText("title size: "
				+ mTiledMap.numPaint,
				100, 350, Color.BLACK, 30);
	}

	private void processInput(float deltaTime) {
		LinkedList<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		LinkedList<KeyEvent> keyEvents = game.getInput().getKeyEvents();
		int lenTouch = touchEvents.size();
		int lenKey = keyEvents.size();

		for (int i = 0; i < lenTouch; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_DOWN) {
				player.touchDownHandle(event, deltaTime);
				continue;
			}
			if (event.type == TouchEvent.TOUCH_DRAGGED) {
				player.touchDraggedHandle(event, deltaTime);
				continue;
			}
			if (event.type == TouchEvent.TOUCH_UP) {
				player.touchUpHandle(event, deltaTime);
				continue;
			}
		}
		
		for (int i = 0; i < lenKey; i++) {
			KeyEvent event = keyEvents.get(i);
			if (event.type == KeyEvent.KEY_DOWN) {
				//if (event.keyCode == )
				game.jumScreen(Screen.SCREEN_MENU);
			}
		}
	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {
		Resrouce.player.dispose();
		Resrouce.mapObject.dispose();
		Resrouce.bulletImage.dispose();
		Resrouce.mummy.dispose();
		Resrouce.bat.dispose();
		Resrouce.boss.dispose();
		BulletManager.getInstance().clear();
		MonsterManager.getInstance().clear();
		DamgeReceiveManager.getInstance().clear();
	}

}
